<template>
  <div class="statistics-page">
    <el-card class="stats-card" shadow="never">
      <template #header>
        <div class="card-header">
          <el-icon size="24" color="#2e7d32"><TrendCharts /></el-icon>
          <span>基础数据统计与可视化</span>
        </div>
      </template>
      
      <el-alert 
        title="平台运营数据概览，包含课题数量、申请情况、匹配成功率等关键指标" 
        type="info" 
        show-icon 
        class="info-alert"
      />
      
      <!-- 核心指标卡片 -->
      <div class="core-stats">
        <el-row :gutter="24">
          <el-col :span="6" v-for="stat in coreStats" :key="stat.label">
            <div class="stat-card" :class="stat.type">
              <div class="stat-icon">
                <el-icon :size="32" :color="stat.iconColor">{{ stat.icon }}</el-icon>
              </div>
              <div class="stat-content">
                <div class="stat-number">{{ stat.value }}</div>
                <div class="stat-label">{{ stat.label }}</div>
                <div class="stat-trend" v-if="stat.trend">
                  <el-icon :color="stat.trend > 0 ? '#4caf50' : '#f44336'">
                    <ArrowUp v-if="stat.trend > 0" />
                    <ArrowDown v-else />
                  </el-icon>
                  <span :class="stat.trend > 0 ? 'trend-up' : 'trend-down'">
                    {{ Math.abs(stat.trend) }}%
                  </span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>

      <!-- 图表区域 -->
      <div class="charts-section">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-card class="chart-card" shadow="never">
              <template #header>
                <div class="chart-header">
                  <span>学科分布统计</span>
                  <el-button type="text" size="small">导出数据</el-button>
                </div>
              </template>
              <div class="chart-content">
                <div class="chart-placeholder">
                  <el-icon size="48" color="#2e7d32"><PieChart /></el-icon>
                  <p>学科分布饼图</p>
                  <p class="chart-desc">展示各学科课题数量分布情况</p>
                </div>
                <div class="data-table">
                  <el-table :data="disciplineData" size="small" border>
                    <el-table-column prop="name" label="学科" width="120" />
                    <el-table-column prop="value" label="数量" width="80" align="center" />
                    <el-table-column prop="percentage" label="占比" width="80" align="center">
                      <template #default="{ row }">
                        <el-progress 
                          :percentage="row.percentage" 
                          :color="getProgressColor(row.percentage)"
                          :show-text="false"
                        />
                        <span class="percentage-text">{{ row.percentage }}%</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="12">
            <el-card class="chart-card" shadow="never">
              <template #header>
                <div class="chart-header">
                  <span>申请状态分布</span>
                  <el-button type="text" size="small">导出数据</el-button>
                </div>
              </template>
              <div class="chart-content">
                <div class="chart-placeholder">
                  <el-icon size="48" color="#4caf50"><Histogram /></el-icon>
                  <p>状态分布柱状图</p>
                  <p class="chart-desc">展示申请各状态的数量分布</p>
                </div>
                <div class="data-table">
                  <el-table :data="statusData" size="small" border>
                    <el-table-column prop="name" label="状态" width="100" />
                    <el-table-column prop="value" label="数量" width="80" align="center" />
                    <el-table-column prop="percentage" label="占比" width="100" align="center">
                      <template #default="{ row }">
                        <el-progress 
                          :percentage="row.percentage" 
                          :color="getStatusColor(row.name)"
                          :show-text="false"
                        />
                        <span class="percentage-text">{{ row.percentage }}%</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 月度趋势 -->
      <div class="trend-section">
        <el-card class="trend-card" shadow="never">
          <template #header>
            <div class="chart-header">
              <span>月度匹配趋势</span>
              <el-select v-model="selectedYear" size="small" style="width: 100px">
                <el-option label="2024年" value="2024" />
                <el-option label="2023年" value="2023" />
              </el-select>
            </div>
          </template>
          <div class="chart-content">
            <div class="chart-placeholder">
              <el-icon size="48" color="#66bb6a"><TrendCharts /></el-icon>
              <p>月度趋势折线图</p>
              <p class="chart-desc">展示各月份匹配数量变化趋势</p>
            </div>
            <div class="trend-data">
              <el-row :gutter="16">
                <el-col :span="4" v-for="month in monthlyData" :key="month.month">
                  <div class="month-item">
                    <div class="month-label">{{ month.month }}</div>
                    <div class="month-value">{{ month.value }}</div>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 导出功能 -->
      <div class="export-section">
        <el-card class="export-card" shadow="never">
          <template #header>
            <div class="chart-header">
              <span>数据导出</span>
            </div>
          </template>
          <div class="export-options">
            <el-space wrap>
              <el-button type="primary" @click="exportProjects">
                <el-icon><Download /></el-icon>
                导出课题数据
              </el-button>
              <el-button type="success" @click="exportApplications">
                <el-icon><Download /></el-icon>
                导出申请数据
              </el-button>
              <el-button type="warning" @click="exportMatches">
                <el-icon><Download /></el-icon>
                导出匹配数据
              </el-button>
              <el-button @click="exportFullReport">
                <el-icon><Document /></el-icon>
                导出完整报告
              </el-button>
            </el-space>
          </div>
        </el-card>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { mockStatistics } from '@/services/mockData'
import { 
  TrendCharts, PieChart, Histogram, ArrowUp, ArrowDown, Download, Document 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const selectedYear = ref('2024')

const stats = mockStatistics

const coreStats = computed(() => [
  {
    label: '课题总数',
    value: stats.totalProjects,
    icon: 'Document',
    iconColor: '#2e7d32',
    type: 'primary',
    trend: 12
  },
  {
    label: '申请总数',
    value: stats.totalApplications,
    icon: 'List',
    iconColor: '#4caf50',
    type: 'success',
    trend: 8
  },
  {
    label: '成功匹配',
    value: stats.totalMatches,
    icon: 'Check',
    iconColor: '#66bb6a',
    type: 'info',
    trend: 15
  },
  {
    label: '匹配成功率',
    value: `${Math.round((stats.totalMatches / stats.totalApplications) * 100)}%`,
    icon: 'TrendCharts',
    iconColor: '#ff9800',
    type: 'warning',
    trend: 5
  }
])

const disciplineData = computed(() => {
  const total = Object.values(stats.projectsByDiscipline).reduce((a, b) => a + b, 0)
  return Object.entries(stats.projectsByDiscipline).map(([name, value]) => ({
    name,
    value,
    percentage: Math.round((value / total) * 100)
  }))
})

const statusData = computed(() => {
  const total = Object.values(stats.applicationsByStatus).reduce((a, b) => a + b, 0)
  return Object.entries(stats.applicationsByStatus).map(([name, value]) => ({
    name: getStatusText(name),
    value,
    percentage: Math.round((value / total) * 100)
  }))
})

const monthlyData = computed(() => {
  return Object.entries(stats.matchesByMonth).map(([month, value]) => ({
    month: month.split('-')[1] + '月',
    value
  }))
})

const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝',
    matched: '已匹配'
  }
  return statusMap[status] || status
}

const getProgressColor = (percentage: number) => {
  if (percentage > 60) return '#4caf50'
  if (percentage > 30) return '#ff9800'
  return '#f44336'
}

const getStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    '待审核': '#ff9800',
    '已通过': '#4caf50',
    '已拒绝': '#f44336',
    '已匹配': '#2196f3'
  }
  return colorMap[status] || '#909399'
}

const exportProjects = () => {
  ElMessage.success('课题数据导出成功')
}

const exportApplications = () => {
  ElMessage.success('申请数据导出成功')
}

const exportMatches = () => {
  ElMessage.success('匹配数据导出成功')
}

const exportFullReport = () => {
  ElMessage.success('完整报告导出成功')
}
</script>

<style scoped>
.statistics-page {
  max-width: 1200px;
  margin: 0 auto;
}

.stats-card {
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
}

.info-alert {
  margin-bottom: 32px;
}

.core-stats {
  margin-bottom: 40px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 24px;
  background: white;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(46, 125, 50, 0.15);
}

.stat-card.primary {
  border-left: 4px solid var(--primary-color);
}

.stat-card.success {
  border-left: 4px solid var(--success-color);
}

.stat-card.info {
  border-left: 4px solid var(--accent-color);
}

.stat-card.warning {
  border-left: 4px solid var(--warning-color);
}

.stat-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: rgba(46, 125, 50, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: var(--primary-color);
  line-height: 1;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.stat-trend {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

.trend-up {
  color: #4caf50;
}

.trend-down {
  color: #f44336;
}

.charts-section {
  margin-bottom: 40px;
}

.chart-card {
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: var(--primary-color);
}

.chart-content {
  padding: 20px 0;
}

.chart-placeholder {
  text-align: center;
  padding: 40px 20px;
  background: var(--background-secondary);
  border-radius: 8px;
  margin-bottom: 20px;
}

.chart-placeholder p {
  margin: 8px 0;
  color: var(--text-secondary);
}

.chart-placeholder .chart-desc {
  font-size: 12px;
  opacity: 0.8;
}

.data-table {
  margin-top: 20px;
}

.percentage-text {
  margin-left: 8px;
  font-size: 12px;
  color: var(--text-secondary);
}

.trend-section {
  margin-bottom: 40px;
}

.trend-card {
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.trend-data {
  padding: 20px 0;
}

.month-item {
  text-align: center;
  padding: 16px 8px;
  background: var(--background-secondary);
  border-radius: 8px;
}

.month-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.month-value {
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
}

.export-section {
  margin-bottom: 24px;
}

.export-card {
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.export-options {
  padding: 20px 0;
}

.el-button {
  border-radius: 8px;
}

.el-button .el-icon {
  margin-right: 6px;
}
</style>


